/**
 * File name: GraphTestDriver.java Author: Willard Wang Create date: Aug 25,
 * 2013
 */
package edu.sjtu.se.prophet.driver;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import edu.sjtu.se.prophet.exception.EdgeExistsException;
import edu.sjtu.se.prophet.exception.NodeExistsException;
import edu.sjtu.se.prophet.exception.NodeNotFoundException;
import edu.sjtu.se.prophet.graph.Edge;
import edu.sjtu.se.prophet.graph.Graph;
import edu.sjtu.se.prophet.graph.Node;
import edu.sjtu.se.prophet.io.gson.EdgeDeserializer;
import edu.sjtu.se.prophet.io.gson.EdgeSerializer;
import edu.sjtu.se.prophet.io.gson.GraphDeserializer;
import edu.sjtu.se.prophet.io.gson.GraphListDeserializer;
import edu.sjtu.se.prophet.io.gson.GraphListSerializer;
import edu.sjtu.se.prophet.io.gson.GraphSerializer;
import edu.sjtu.se.prophet.io.gson.NodeDeserializer;
import edu.sjtu.se.prophet.io.gson.NodeSerializer;

/**
 * @author deathspeeder
 * 
 */
public class GraphTestDriver {

    /**
     * @param args
     */
    public static void main(String[] args) {
        List<Graph> list = new ArrayList<Graph>();
        Graph graph = new Graph();
        list.add(graph);
        graph.getMeta().put("x", "x");
        try {
            graph.addNode("a");
            graph.addNode("b");
            graph.addNode("c");
            graph.addNode("d");
            graph.addNode("e");
            graph.addNode("f");
            graph.findNode("a").getMetrics().put("bug", 12);
            graph.findNode("b").getMetrics().put("bug", 12);
            graph.findNode("c").getMetrics().put("bug", 12);
            graph.findNode("d").getMetrics().put("bug", 12);
            graph.findNode("e").getMetrics().put("bug", 12);
            graph.findNode("f").getMetrics().put("bug", 12);
            graph.addEdge("f", "a");
            graph.addEdge("a", "f");
            graph.addEdge("c", "a");
            graph.addEdge("a", "c");
            graph.addEdge("f", "c");
            graph.addEdge("c", "f");
            // graph.addEdge("a", "c");
            // graph.addEdge("c", "f");
            // graph.addEdge("a", "b");
            // graph.addEdge("b", "d");
            // graph.addEdge("b", "e");
        } catch (NodeExistsException e) {
            e.printStackTrace();
        } catch (NodeNotFoundException e) {
            e.printStackTrace();
        } catch (EdgeExistsException e) {
            e.printStackTrace();
        }

        System.out.println(graph);

        // ObjectOutputStream oos;
        // try {
        // oos = new ObjectOutputStream(new FileOutputStream("graph.per"));
        // oos.writeObject(list);
        // oos.close();
        //
        // ObjectInputStream ois = new ObjectInputStream(new FileInputStream(
        // "graph.per"));
        // list = (List<Graph>) ois.readObject();
        // graph = list.get(0);
        // } catch (Exception e) {
        // e.printStackTrace();
        // }

        Gson gson = new GsonBuilder()
                .registerTypeAdapter((new TypeToken<List<Graph>>() {
                }).getType(), new GraphListSerializer())
                .registerTypeAdapter(Graph.class, new GraphSerializer())
                .registerTypeAdapter(Node.class, new NodeSerializer())
                .registerTypeAdapter(Edge.class, new EdgeSerializer()).create();
        String json = gson.toJson(list);
        System.out.println(json);

        gson = new GsonBuilder()
                .registerTypeAdapter((new TypeToken<List<Graph>>() {
                }).getType(), new GraphListDeserializer())
                .registerTypeAdapter(Graph.class, new GraphDeserializer())
                .registerTypeAdapter(Node.class, new NodeDeserializer())
                .registerTypeAdapter(Edge.class, new EdgeDeserializer())
                .create();
        list = gson.fromJson(json, (new TypeToken<List<Graph>>() {
        }).getType());

        System.out.println(list.get(0));

        // List<Node> nodes = new ArrayList<Node>(graph.getNodes());
        // double[][] distance = GraphUtil.distanceByDijkstra(nodes);
        // for (int i = 0; i < distance.length; i++) {
        // System.out.print(nodes.get(i));
        // for (int j = 0; j < distance.length; j++) {
        // System.out.print(" " + distance[i][j]);
        // }
        // System.out.println();
        // }
    }

}
